package com.zhiche.lisa.lspm.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.dao.model.sys.Permission;
import com.zhiche.lisa.lspm.dao.model.sys.Role;
import com.zhiche.lisa.lspm.dao.model.sys.UserRole;
import com.zhiche.lisa.lspm.dao.model.trailer.bo.TrailerInfoLineBO;
import com.zhiche.lisa.lspm.service.sys.IRoleService;
import com.zhiche.lisa.lspm.service.sys.IUserRoleService;
import com.zhiche.lisa.lspm.vo.sys.RolePermRequestVO;
import com.zhiche.lisa.lspm.vo.sys.RoleUserVO;
import com.zhiche.lisa.lspm.vo.vehicle.TrailerInfoLineVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 用户角色
 *
 * @author hxh
 * @since 2018-09-28
 */
@RestController
@RequestMapping("/userRole")
@Api(value = "/userRole", description = "用户角色", tags = {"用户角色"})
public class UserRoleController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserRoleController.class);

    @Autowired
    private IUserRoleService userRoleService;


    @PostMapping(value = "/userRoleList")
    @ApiOperation(value = "用户角色列表", notes = "用户角色列表", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<Page<RoleUserVO>> userRoleList(@RequestBody Page<RoleUserVO> page, @RequestHeader("Authorization") String authorization) {
        LOGGER.info("UserRoleController.userRoleList（用户角色列表） params : {} ", page);

        RestfulResponse<Page<RoleUserVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(),CommonEnum.SUCCESS.getText(),null);

        try {
            Page<RoleUserVO> pageVo = userRoleService.queryRolePermission(page);
            result.setData(pageVo);
        } catch (BaseException e) {
            LOGGER.error("UserRoleController.userRoleList ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("UserRoleController.userRoleList ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

    @PostMapping(value = "/addUserRole")
    @ApiOperation(value = "添加用户角色", notes = "添加用户角色", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<Objects> addUserRole(@RequestBody UserRole userRole) {
        LOGGER.info("UserRoleController.addUserRole（添加用户角色） params : {} ", userRole);

        RestfulResponse<Objects> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(),CommonEnum.SUCCESS.getText(),null);

        try {
            userRoleService.addAndUpdata(userRole);
        } catch (BaseException e) {
            LOGGER.error("UserRoleController.addUserRole ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("UserRoleController.addUserRole ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

}

